var express = require('express');
var router = express.Router();
var md5 = require('md5');
var request = require('request');
var consts = require('../common/constants');

router.get('/', function(req, res, next) {
    res.render('app_advertisement', {title: 'app广告管理'})
});
//图片列表
router.get('/list', function(req, res, next) {
	var top = req.cookies.token;
	var params = {
        e:{},
    };
    var token = JSON.stringify(params)+ "|" + consts.MD5_String;
    token = md5(token);
    var url = consts.APP_HOST + '/api/teacher/selectList?token=' + token;
    var options = {
    	headers: {
		  'X-Authorization': 'Bearer '+top,
		},
        url: url,
        method: 'POST',
        json: true,
        body: params
    };
    console.log(top);
	request(options, function(err, httpResponse, body) {
        res.send(body.MobileBody.pageInfo)
    ;})
});
//app新增页面
router.get('/new_advertisement', function(req, res, next) {
    res.render('new_advertisement', {title: 'new_advertisement'});
});
//app图片新增
router.post('/new', function(req, res, next) {
	var params = {
    };
    var token = JSON.stringify(params)+ "|" + consts.MD5_String;
    token = md5(token);
    var url = consts.APP_HOST + '/editor/upload?token=' + token;
    var options = {
        url: url,
        method: 'POST',
        json: true,
        body: params
    };
    request(options, function(err, httpResponse, body) {
    	console.log(body);
    	res.send(body)
  	})
});
//app图片编辑
router.get('/editor_advertisement', function(req, res, next) {
    res.render('editor_advertisement', {title: 'editor_advertisement'});
});
module.exports = router;